package com.yishun.springboot.dao;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.File;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 设备openapi模块对接的案例
 */

@Slf4j
public class OpenAPITest {

    private String api_key = "xxx";
    private String timestamp = "2019-07-28 15:31:14";
    private String api_version = "1.0";
    private String engCode = "xxx";
    private String projectCode = "xxx";


    /**
     * signature生成
     */
    @Test
    public void signatur() {
        String signature = getSignature(api_key, "1.0", engCode, projectCode, timestamp, "body");
        log.info(signature);

    }

    /**
     * 上传图片√
     */
    @Test
    public void uploadImage() {
    	log.info("uploadImage");
        HashMap<String, Object> paramMap = new HashMap<>();
        //文件
        File file = new File("C:\\Users\\YS\\Desktop\\test.png");
        if(file.length()!=0) log.info("file is exis");
        paramMap.put("file", file);
        paramMap.put("api_key", api_key);
        String result = HttpUtil.post("http://127.0.0.1:80/uploadfile-openpai/uploadImage", paramMap);
        log.info(result);//9c2c779d89c64e07b21f27972f96b467
    }

    /**
     * 车辆信息√
     */
    @Test
    public void uploadData() {
        String url = "http://127.0.0.1:80/openapi/vehicle/uploadData";
        JSONObject param = new JSONObject();
        JSONObject body = new JSONObject();
        param.put("api_key", api_key);
        param.put("project_code", projectCode);
        param.put("eng_code", engCode);
        param.put("api_version", api_version);
        param.put("timestamp", timestamp);


        body.put("device_no", "123");
        body.put("type", "1");
        body.put("plate_number", "粤BBG217");
        body.put("direction", "1");
        body.put("gate_no", "4B452A10F2D6C20F66050AF8");
        body.put("region_no", "201807091340046027940");
        body.put("region_type", "1");
        body.put("lift_time", "2018-12-03 15:20:30");

        //图片信息
        JSONObject image1 = new JSONObject();
        image1.put("img_type", "1");
        image1.put("img_data", "9c2c779d89c64e07b21f27972f96b467");
        JSONObject image2 = new JSONObject();
        image2.put("img_type", "2");
        image2.put("img_data", "009c7438853743cfb6123c90fe15fb11");
        List<JSONObject> images = new ArrayList<>();
        images.add(image1);
        images.add(image2);


        body.put("images", images);

        List<JSONObject> bodyList = new ArrayList<>();
        bodyList.add(body);
        param.put("body", bodyList);//body

        String jsonBody = JSONObject.toJSONString(bodyList);//body的json字符串
        String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
        param.put("signature", signature);

        log.info(param.toJSONString());
//        发送请求
        String info = HttpUtil.post(url, param.toJSONString());
        log.info(info);
    }


    /**
     * 获取签名
     *
     * @param: [apiKey, apiVersion, engCode, projectCode, timestamp, body]
     * @return: java.lang.String
     */
    public static String getSignature(String apiKey, String apiVersion,
                                      String engCode, String projectCode,
                                      String timestamp, String body) {
        String str4MD5 = String.format("api_key%sapi_version%sbody%seng_code=%sproject_code=%stimestamp%s",
                apiKey, apiVersion, body.isEmpty() ? "{}" : body, engCode, projectCode, timestamp);
        return getMD5Code(str4MD5);
    }

    public static String getMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            Charset charset = Charset.forName("UTF-8");
            resultString = byteToString(md.digest(strObj.getBytes(charset)));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 全局数组
    private final static String[] strDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "A", "B", "C", "D", "E", "F"};
}
